import frappe
from frappe.utils import floor
from frappe import _


def po_validate(doc, method):
    #抽取物料号
    if not frappe.db.has_column('Item','min_pack_qty'):return
    item_code_set = {item.item_code for item in doc.items}
    qty_list = frappe.get_all("Item", 
                            filters = {'item_code':('in', list(item_code_set) ),
                                        'min_pack_qty': ('>',0)
                                        },
                            fields =['item_code','purchase_uom','min_pack_qty'])
    qty_dict = {(item.item_code, item.purchase_uom):item.min_pack_qty for item in qty_list}
    item_code_list = [item.item_code for item in qty_list]
    items = [item for item in doc.items if item.item_code in item_code_list]
    for item in items:
        min_pack_qty = qty_dict.get((item.item_code, item.uom), 0)
        if min_pack_qty:
            Quotient = item.qty / min_pack_qty
            rounded_quotient = floor(Quotient)
            if Quotient > rounded_quotient:
                old_qty = item.qty
                item.qty = (rounded_quotient + 1) * min_pack_qty
                item.stock_qty = item.qty * item.conversion_factor
                frappe.msgprint(_('Item {0} Qty {1} changed to {2} due to min pack qty {3}'
                    .format(item.item_code, old_qty,item.qty, min_pack_qty)))

@frappe.whitelist()
def sales_order_save_submit(doc, method):
    doc.submit()